Namespace Mouse

    Module mMouse

        Public Sub Init()
            messageQueue = New Queue
        End Sub

        Public Sub DoInputs()

            SyncLock messageQueue

                Dim msg As MouseMessage
                Dim i As Integer
                Dim c As Integer = messageQueue.Count

                Do Until i = c

                    msg = messageQueue.Dequeue()

                    Select Case msg.type

                        Case MouseInputType.MouseDown
                            Mouse.bIsDown = True

                        Case MouseInputType.MouseUp
                            If Not MovingObj Is Nothing Then
                                Try
                                    MovingObj.UnFocus()
                                Catch ex As Exception
                                End Try
                                MovingObj = Nothing
                            End If
                            Mouse.bIsDown = False

                    End Select

                    Windows.MouseInput(msg.type, msg.args)

                    i += 1

                Loop

            End SyncLock

        End Sub

        Public Sub Input(ByRef type As MouseInputType, ByRef e As MouseEventArgs)

            If messageQueue Is Nothing Then Exit Sub

            Select Case type

                Case MouseInputType.MouseMove
                    Mouse.XCoord = e.X
                    Mouse.YCoord = e.Y

            End Select

            Dim msg As MouseMessage
            msg.type = type
            msg.args = e

            SyncLock messageQueue

                For Each tmp As MouseMessage In messageQueue
                    If tmp.type = msg.type Then
                        GoTo found
                    End If
                Next
                messageQueue.Enqueue(msg)

found:

            End SyncLock

        End Sub

        Public Sub LostFocus()

            Mouse.bIsDown = False

        End Sub

        Public Sub SetHoverFlag(ByRef rect As Rectangle, ByRef flag As Boolean)

            flag = rect.Contains(Mouse.XCoord, Mouse.YCoord)

        End Sub

        Public Sub SetFocusFlag(ByRef rect As Rectangle, ByRef flag As Boolean)

            If bIsDown Then
                SetHoverFlag(rect, flag)
                If flag Then

                    If Not bLastFocusRef Is Nothing Then
                        Try
                            bLastFocusRef = False
                        Catch
                        End Try
                    End If
                    bLastFocusRef = flag

                End If
            End If

        End Sub

        Public Function IsOver(ByRef rect As Rectangle) As Boolean

            Dim tmp As Boolean
            SetHoverFlag(rect, tmp)
            Return tmp

        End Function

    End Module

End Namespace